package com.qeegoo.b2bautozityp.moudle.goods.viewmodel;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.databinding.ObservableField;
import android.util.Log;
import android.view.View;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.kelin.mvvmlight.messenger.Messenger;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.moudle.goods.model.GoodsManagerBean;
import com.qeegoo.b2bautozityp.moudle.goods.view.GoodsManagerFragment;
import com.qeegoo.b2bautozityp.moudle.goods.view.GoodsSearchActivity;
import com.qeegoo.b2bautozityp.utils.AdapterInterface;

import java.util.ArrayList;
import java.util.List;

import base.lib.util.ActivityManager;
import base.lib.util.NavigateUtils;
import base.lib.util.ToastUtils;

/**
 * Created by lk on 2017/9/20.
 */

public class GoodsManagerViewModel {

    public RequestApi requestApi;
    private int fragmentType;
    private String goodsType;

    public ObservableField<Integer> navgationLeftIcon = new ObservableField<>(R.mipmap.ic_arrow_left);
    public ObservableField<Integer> visible = new ObservableField<>(View.VISIBLE);
    public ObservableField<Integer> emptyVisible = new ObservableField<>(View.GONE);
    public ObservableField<Integer> bottomVisible = new ObservableField<>(View.GONE);
    public ObservableField<String> leftText = new ObservableField<>("批量管理");
    public ObservableField<String> bottomText = new ObservableField<>("批量上架");
    public ObservableField<Boolean> bottomSelect = new ObservableField<>(false);
    private List<String> selectArray = new ArrayList<>();
    private AdapterInterface<GoodsManagerBean> adapterInterface;
    public ReplyCommand selectAllCommand = new ReplyCommand(() -> {

        GoodsPageData data = new GoodsPageData(2, !bottomSelect.get());
        Messenger.getDefault().send(data, GoodsManagerFragment.class.getSimpleName());//通知 刷新recycler
//        bottomSelect.set(!bottomSelect.get());
    });
    public ReplyCommand bottomCommand = new ReplyCommand(() -> {

        String goodsId = selectArray.toString().replace("[", "").replace("]", "").replaceAll("\\s*", "");
        Log.e("TYP", "size-->" + selectArray.size() + goodsId);
        requestApi.underBatchGoods(HttpParams.paramUpdateBatchGoods(goodsId))
                .map(new RetrofitService.HttpResultFunc())
                .compose(RetrofitService.applySchedulers())
                .subscribe(new ProgressSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        ToastUtils.showToast("下架成功！");
                        getDatas("1", goodsType, "");
                    }
                });

    });
    public ReplyCommand gotoSearchCommand = new ReplyCommand(() -> {

        NavigateUtils.startActivity(GoodsSearchActivity.class);

    });
    public ReplyCommand leftCommon = new ReplyCommand(() -> {
        ActivityManager.getActivity().finish();
    });
    public ReplyCommand rightCommon = new ReplyCommand(() -> {
        boolean isShow = false;
        if ("取消".equals(leftText.get())) {
            isShow = false;
            leftText.set("批量管理");
        } else {
            isShow = true;
            leftText.set("取消");
        }
        if (isShow) {
            GoodsPageData data = new GoodsPageData(1, isShow);
            Messenger.getDefault().send(data, GoodsManagerFragment.class.getSimpleName());//通知 刷新recycler
        } else {
            GoodsPageData data = new GoodsPageData(3, isShow);
            Messenger.getDefault().send(data, GoodsManagerFragment.class.getSimpleName());//通知 刷新recycler
        }


    });

    public GoodsManagerViewModel(RequestApi api) {
        this.requestApi = api;
    }


    public void setAdapterInterface(AdapterInterface<GoodsManagerBean> orderResultBeanAdapterInterface) {
        this.adapterInterface = orderResultBeanAdapterInterface;
    }

    public void getDatas(String pageNo, String goodsType, String words) {
        this.goodsType = goodsType;
        requestApi.goodsList(HttpParams.paramGoodsLists(pageNo, 10 + "", goodsType, words))
                .map(new RetrofitService.HttpResultFunc<>())
                .compose(RetrofitService.applySchedulers())
                .subscribe(goodsManagerBean -> {
                    adapterInterface.callBackData(goodsManagerBean);
                }, throwable -> {
                    throwable.getMessage();
                });
    }

    public void setArray(List<String> selectArray, int allCount) {
        this.selectArray = selectArray;

        bottomSelect.set(selectArray.size() == allCount);
    }

    public void setFragmentType(int state) {
        this.fragmentType = state;

        switch (state) {
            case Constants.OrderType_1:
                bottomText.set("批量下架");
                break;

            case Constants.OrderType_2:
                bottomText.set("批量上架");
                break;

            case Constants.OrderType_3:
                bottomText.set("批量发布");
                break;

            default:
                break;
        }
    }

    //商品页面 数据传递bean
    public static class GoodsPageData {
        public int dataType; //1批量管理2全部选择
        public boolean dataBoolean;

        public GoodsPageData(int dataType, boolean dataBoolean) {
            this.dataType = dataType;
            this.dataBoolean = dataBoolean;
        }
    }

}
